<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avater_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域             ref=xx xx表示为表单的实例对象    $refs对象挂载在vm身上  xx为它的属性      -->
      <el-form :model="loginForm" class="demo-form-inline" :rules="loginFormRules" ref="loginForm">
        <!-- 用户名 -->
        <!-- <el-form-item label-width="70px" label="用户名："> -->
        <el-form-item prop="username">
          <!-- 导入阿里字体图标 实现 input框头像 -->
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <!-- <el-form-item label-width="70px" label="密码:___"> -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        // 登录表单数据
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset () {
      // 实现重置 ref 定义的对象 可以拿到整个表单的引用对象
      this.$refs.loginForm.resetFields()
    },
    login () {
      this.$refs.loginForm.validate(async (boolean, vl) => {
        if (!boolean) return this.$message.error('请正确填写用户名和密码')
        const { data: res } = await this.$http.post('login', this.loginForm) // 发送请求并且 解构赋值接受返回参数
        if (res.meta.status !== 200) return this.$message.error('登录失败' + res.meta.msg)
        this.$message.success('登录成功')
        localStorage.setItem('token', res.data.token) // 试着本地token
        this.$router.push('/home') // 跳转到主页面
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  .avater_box {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px #ddd;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.demo-form-inline {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0;
  padding: 10px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
